{% extends 'layout/manage.html' %}
{% load static %}
{% block title %}
    wiki
{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.preview.min.css' %}">

    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 5px;
        }

        .panel-default {
            margin-top: 10px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <i class="fa fa-book" aria-hidden="true"></i> wiki文档
                </div>

                <div class="function">
                    <a type="button" class="btn btn-success btn-xs"
                       href="{% url 'wiki_add' project_id=request.tracer.project.id %}">
                        <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建
                    </a>
                    <!--如果有文章预览内容/文章wiki_id，代表查看该文章了-->
                    {% if wiki_obj %}
                        <a type="button" class="btn btn-primary btn-xs"
                           href="{% url 'wiki_edit' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}">
                            <i class="fa fa-edit" aria-hidden="true"></i> 编辑
                        </a>
                        <a type="button" class="btn btn-danger btn-xs"
                           href="{% url 'wiki_delete' project_id=request.tracer.project.id %}?wiki_id={{ wiki_id }}">
                            <i class="fa fa-trash" aria-hidden="true"></i> 删除
                        </a>
                    {% endif %}

                </div>
            </div>
            <!--面板内容-->
            <div class="panel-body">
                <!--文章标题-->
                <!--
                            <li>目录1
                               <ul></ul>
                           </li>

                           <li>目录2
                               <ul>
                                   <li>目录2.1
                                       <ul></ul>
                                   </li>
                               </ul>
                           </li>
                           -->
                <div class="col-sm-3 title-list">
                    <ul id="multilevelDirectory">

                    </ul>

                </div>
                <!--文章内容-->
                <div class="col-sm-9 content">
                    <div style="text-align: center;margin-top: 50px;">
                        {% if wiki_obj.id %}
                            <div id="previewMarkdown">
                               <Textarea>
                                       {{ wiki_obj.content }}
                                </Textarea>
                            </div>
                        {% else %}
                            <h4>{{ request.tracer.project.name }}</h4>
                            <a href="{% url 'wiki_add' project_id=request.tracer.project.id %}">
                                <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建文章
                            </a>
                        {% endif %}
                    </div>
                </div>
                <div></div>
            </div>
        </div>
    </div>

{% endblock %}


{% block js %}
    <script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/jquery.flowchart.min.js' %}"></script>
    <script>
        $(function () {
            initMultilevelDirectory();
            initPreivewMarkdown();

        })

        // 预览
        function initPreivewMarkdown() {
            editormd.markdownToHTML("previewMarkdown", {
                htmlDecode: "style,script,iframe"
            });
        }

        // 多级目录
        function initMultilevelDirectory() {
            $.ajax({
                url: "{% url 'multilevel_directory' project_id=request.tracer.project.id %}",
                type: "GET",
                dataType: "JSON",
                success: function (ret) {
                    // console.log(ret.data)  {parent_id: null, title: '’看'} {parent_id: 1, title: '哦i建安费奢侈品'}
                    // 建立标签
                    if (ret.data) {
                        // 有数据
                        $.each(ret.data, function (key, value) {
                            var HREF = "{% url 'wiki' project_id=request.tracer.project.id %}" + '?wiki_id=' + value.id;
                            var li = $('<li>').attr('id', 'id_' + value.id,).append($('<a>').text(value.title).attr('href', HREF)).append($('<ul>'));
                            // key 索引 value 字典
                            if (!value.parent_id) {
                                // 没有父级，添加一个li
                                $('#multilevelDirectory').append(li)
                            } else {
                                // 找到对应的parent_id对应的id号
                                $('#id_' + value.parent_id).children('ul').append(li)
                            }
                        })
                    } else {
                        // 没有数据
                    }
                }
            })
        }
    </script>
{% endblock %}